1009C - Annoying Present - CodeForces Solution


greedy math *1700

Please click on ads to support us..

C++ Code:

// Code by Brijesh Verma(_bverma512)
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define endl "\n"
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vll vector<long long>
#define vin(n,v) for(int i = 0; i < n; i++) {ll a; cin >>a; v.push_back(a);}
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '"' << x << '"';}
void __print(const string &x) {cerr << '"' << x << '"';}
void __print(bool x) {cerr << (x ? 1 : 0);}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << "{"; __print(x.first); cerr << ","; __print(x.second); cerr << "}";}
template<typename T>
void __print(const T &x) {int f = 0; cerr << "{"; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
//vector<ll> p(1e7+7,1);
//void sieve(){
//    p[0]=p[1]=0;
//    for(ll i=2;i<=1e7+7;i++){
//       for(ll j=i*2;j<=1e7+7;j+=i){
//            // if(p[j]){p[j]=i;} // lowest prime
//           p[j]=0; 
//        }
//    }
//}
ll mod = 998244353;
ll inv(ll i) {if (i == 1) return 1; return (mod - ((mod / i) * inv(mod % i)) % mod) % mod;}
ll mod_mul(ll a, ll b) {a = a % mod; b = b % mod; return (((a * b) % mod) + mod) % mod;}
ll pw(ll x,ll y){ll cnt = 1;x = x%mod;while(y > 0){if(y%2!=0){cnt = (cnt*x)%mod;}y /=2;x = ((x%mod)*(x%mod))%mod;}return cnt;}

void solve_tc(int tc){

    ll n,m;
    cin>>n>>m;

    ll sum =0;
    ll y = n/2;
    for(int i=0;i<m;i++){
        ll x,d;
        cin>>x>>d;

        sum += n*x;

        if(d>=0){
            sum += d*((n-1)*n)/2;
        }else{
            if(n%2){
                sum += d*((y+1)*y);
            }else sum += d*((y*(y-1))/2+(y*(y+1))/2);
        }

    }

    long double ans =sum/(1.0*n);

    cout<<fixed<<setprecision(15)<<ans<<endl;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int testcases=1;
    // cin>>testcases;
    for(int tt=1;tt<=testcases;tt++){
        solve_tc(testcases);
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

50. Pow(x, n)
43. Multiply Strings
34. Find First and Last Position of Element in Sorted Array
33. Search in Rotated Sorted Array
17. Letter Combinations of a Phone Number
5. Longest Palindromic Substring
3. Longest Substring Without Repeating Characters
1312. Minimum Insertion Steps to Make a String Palindrome
1092. Shortest Common Supersequence
1044. Longest Duplicate Substring
1032. Stream of Characters
987. Vertical Order Traversal of a Binary Tree
952. Largest Component Size by Common Factor
212. Word Search II
174. Dungeon Game
127. Word Ladder
123. Best Time to Buy and Sell Stock III
85. Maximal Rectangle
84. Largest Rectangle in Histogram
60. Permutation Sequence
42. Trapping Rain Water
32. Longest Valid Parentheses
Cutting a material
Bubble Sort
Number of triangles
AND path in a binary tree
Factorial equations
Removal of vertices
Happy segments
Cyclic shifts